/**
 * 监播管理api
 */
angular.module('imsApp').factory('copyScreenManager', ['$http', 'api','paramsManager','FileUploader', function ($http,api ,paramsManager,FileUploader) {
    var copyScreenManager = {
        /**
         * 获取拷屏计划列表
         params:[pageNo,pageSize]//顺序不能错
         search{}
         */
        copyScreenList: function (params, query) {
            return $http.get(api+"/ims/copyScreenCampaigns/" + paramsManager.params(params) + paramsManager.query(query)).then(function (result) {
                return result;
            });
        },
        /**
         * 拷屏计划详情
         * @param campaignId
         * @returns {"c":"s","campaignId":1}
         */
        copyScreenDetail: function (params, query) {
            return $http.get(api+"/ims/copyScreenCampaigns/" + paramsManager.params(params) + '/apps' + paramsManager.query(query)).then(function (result) {
                return result;
            });
        },
        /**
         * 查看拷屏
         * @param campaignId
         * @param appId
         * @returns {"c":"s","campaignId":1}
         */
        copyScreenView: function (campaignId,appId) {
            return $http.get(api+"/ims/copyScreenCampaigns/" +campaignId + '/apps/' + appId + '/copyScreens').then(function (result) {
                return result;
            });
        },
        /**
         * 上传图片url数组
         * @param campaignId
         * @param appId
         * @returns {"c":"s","campaignId":1}
         */
        saveCopyScreen:function (campaignId,appId,imags) {
            return $http.post(api+"/ims/copyScreenCampaigns/"+campaignId+"/apps/"+appId+"/copyScreens",{urlArray:imags}).then(function (result) {
                return result;
            });
        },

        getImage: function ($scope) {
            var MAXSIZE = 1;//单张图片最大大小：单位M
            var MAXITEM = 3;//上传张数限制
            var image = $scope.uploaderImage = new FileUploader({
                alias:"photoFile",
                queueLimit: 3,//文件个数
                // removeAfterUpload: true,//上传后删除文件 */
                filters: [{
                    name: 'photoFile',//后台接收字段
                    fn: function (item /*{File|FileLikeObject}*/, options) {
                        if (!/\.(jpg|jpeg|png|JPG|PNG)$/.test(item.name)) {
                            $scope.checkImage = "E";
                            $scope.imageMsg = "最多只能上传" + MAXITEM + "张png、jpg格式且每张小于" + MAXSIZE + "MB的图片";
                            return false;
                        }
                        if (this.queue.length > MAXITEM - 1) {
                            $scope.checkImage = "E";
                            $scope.imageMsg = "最多只能上传" + MAXITEM + "张png、jpg格式且每张小于" + MAXSIZE + "MB的图片";
                            return false;
                        } else {
                            if (item.size > MAXSIZE * 1024 * 1024) {
                                $scope.checkImage = "E";
                                $scope.imageMsg = "最多只能上传" + MAXITEM + "张png、jpg格式且每张小于" + MAXSIZE + "MB的图片";
                                return false;
                            } else {
                                //去重
                                if (this.queue.length > 0) {
                                    var isExsit = false;
                                    for (var i = 0; i < this.queue.length; i++) {
                                        if (this.queue[i].file.name == item.name) {
                                            isExsit = true;
                                            break;
                                        }
                                    }
                                    if (isExsit) {
                                        return false;
                                    } else {
                                        $scope.uploadSuccess = false;
                                        return true;
                                    }
                                } else {
                                    $scope.uploadSuccess = false;
                                    return true;
                                }
                            }
                        }
                    }
                }],
                url: api+"/ims/copyScreen/image",
                headers: {'X-Auth-Token': $scope.userInfo.token}

            });
            return image;
        },
        uploadFile: function ($scope, image, id) {
            if (image.queue.length > 0) {
                //上传
                image.uploadAll();
                image.onErrorItem = function (fileItem, response, status, headers) {
                    alert("网络出现故障，上传失败,请刷新重新添加");   //未达到后台上传失败
                };
                //成功上传
                var successNo = 0;
                image.onSuccessItem = function (fileItem, response, status, headers) {
                    if (response.c == "s") {
                        successNo++;
                        fileItem.successUrl=response.fileVO.webUrl;
                        $scope.imagesUrl.push(response.fileVO.webUrl);
                        if (successNo == image.queue.length) {
                            $scope.uploadSuccess = true;
                        }
                    } else if (response.c == 'f') {
                        fileItem.isSuccess = false;
                        alert("上传  " + fileItem.file.name + " 失败，请删除后重新添加上传");
                    } else {
                        fileItem.isSuccess = false;
                        alert(response.i);
                    }
                };
            }
        }

    };

    return copyScreenManager;
}]);
